#include "editor.h"
#include <QtGui>

Editor::Editor(QWidget* parent): QsciScintilla(parent)
{
        //this->setupDefaults();
        //this->zoomOut(2);
        this->setCaretLineVisible(1);
        this->setCaretLineBackgroundColor(QColor(206, 217, 234,0));
        m_openedFilePath = QString();
}

void Editor::setupDefaults()
{
    setupDefaultLexer();

        this->setFolding(QsciScintilla::CircledTreeFoldStyle );
        this->setIndentation(true,4);
        this->setAutoIndent(true);
    //setup end-of-line mode
    #if defined Q_WS_X11
    this->setEolMode(QsciScintilla::EolUnix);
    #elif defined Q_WS_WIN
    this->setEolMode(QsciScintilla::EolWindows);
    #elif defined Q_WS_MAC
    this->setEolMode(QsciScintilla::EolMac);
    #endif

    //setup auto-indentation
    this->setAutoIndent(true);
    this->setIndentationGuides(true);
    this->setIndentationsUseTabs(true);
    this->setIndentationWidth(4);

    //setup margins
    this->setMarginsBackgroundColor(QColor(255, 255, 128));
    this->setMarginLineNumbers(1, true);
    this->setMarginWidth(1, QString("10000"));

    //setup brace matching
    this->setBraceMatching(QsciScintilla::SloppyBraceMatch);
    this->setMatchedBraceBackgroundColor(QColor(148, 226, 61));
    this->setUnmatchedBraceForegroundColor(Qt::black);

    //setup autocompletion
    this->setAutoCompletionSource(QsciScintilla::AcsAll);
    this->setAutoCompletionCaseSensitivity(true);
    this->setAutoCompletionReplaceWord(true);
    this->setAutoCompletionShowSingle(true);
    this->setAutoCompletionThreshold(2);

    this->setUtf8(true);
}

void Editor::setupDefaultLexer()
{
    //if(lexer) delete lexer;
    lexer = new QsciLexerCPP(this);
    lexer->setFoldComments(true);
    lexer->setFoldAtElse(true);
    lexer->setFoldPreprocessor(true);

    #if defined Q_WS_WIN
    lexer->setFont(QFont("Courier New", 10, QFont::Bold));
    #elif defined Q_WS_X11
    lexer->setFont(QFont("Monospace"));
    #endif

    lexer->setAutoIndentStyle(QsciScintilla::AiOpening);
    this->setLexer(lexer);
}

QString Editor::filePath()
{
    return this->m_openedFilePath;
}

void Editor::setFilePath(QString filePath)
{
    this->m_openedFilePath = filePath;
    qDebug(this->filePath().toStdString().c_str());
}

bool Editor::save()
{
    qDebug(this->filePath().toStdString().c_str());
    if (this->filePath().isEmpty()) {
        return saveAs();
    } else {
        return saveFile(filePath());
    }
}

bool Editor::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this);
    if (fileName.isEmpty())
        return false;

    return saveFile(fileName);
}

bool Editor::saveFile(QString fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly)) {
        QMessageBox::warning(this, tr("Application"),
                             tr("Cannot write file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return false;
    }

    QTextStream out(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    out << this->text();
    QApplication::restoreOverrideCursor();
    setCurrentFile(fileName);
    return true;
}



void Editor::setCurrentFile(const QString &fileName)
{
    this->m_openedFilePath = fileName;
    this->setModified(false);
    setWindowModified(false);

    QString shownName;
}

void Editor::setFont(const QFont& f)
{
    this->lexer->setFont(f);
}

void Editor::maybeSave()
{
}
